// import { awaitWrap, confirmAction } from '@/assets/js/tool'
import { awaitWrap, confirmAction, formatDate, limitStr, strToNum } from '@src/assets/js/tool'
import { message, Space } from 'antd'
import React from 'react'
import { delRequest } from './ajax'

async function delHandle (id, callback) {
  const h = message.loading('删除中')
  const [e] = await awaitWrap(delRequest(id))
  h()
  if (e === null) {
    message.success('删除成功')
    callback()
  }
}

export function getColumns (common, setVisible, setModifyData, refreshFn) {
  return [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      align: 'center',
      width: '60px'
    },
    {
      title: '标题',
      dataIndex: 'title',
      key: 'title',
      width: 100
    },
    // {
    //   title: '简介',
    //   dataIndex: 'summary',
    //   key: 'summary',
    //   width: 100
    // },
    // {
    //   title: '可见机构',
    //   dataIndex: 'visible_structure',
    //   key: 'visible_structure',
    //   width: 100
    // },
    {
      title: '可见类型',
      dataIndex: 'visible_type',
      key: 'visible_type',
      width: 100
    },
    {
      title: '发布日期',
      dataIndex: 'release_datetime',
      key: 'release_datetime',
      width: 100,
      render: (v) => {
        v = strToNum(v)
        if (v) {
          return formatDate(v, 'YYYY-MM-DD HH:mm:ss')
        }
      }
    },
    {
      title: '失效日期',
      dataIndex: 'expiry_datetime',
      key: 'expiry_datetime',
      width: 100,
      render: (v) => {
        v = strToNum(v)
        if (v) {
          return formatDate(v, 'YYYY-MM-DD HH:mm:ss')
        }
      }
    },
    {
      title: '操作',
      dataIndex: 'index',
      key: 'operate',
      width: 60,
      render: (value, line) => {
        return (
          <Space size='middle'>
            <a onClick={() => { setModifyData(line); setVisible(true) }}>修改</a>
            <a onClick={() => {
              if (line.url) {
                window.open(line.url)
              } else {
                common.$openPage({ id: `caselib-detail-${line.id}`, title: `案例详情：${limitStr(line.title, 10)}`, componentName: 'CaselibDetail', meta: { id: line.id, title: line.title } })
              }
            }}
            >详情
            </a>
            <a onClick={() => { confirmAction('您确定要删除吗？', () => { delHandle(line.id, refreshFn) }) }}>删除</a>
          </Space>
        )
      }
    }
  ]
}
